package pers.vic.upload.socket.handler;

import cn.xuqiudong.common.util.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.socket.BinaryMessage;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.AbstractWebSocketHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;
import pers.vic.upload.constant.SessionAttr;
import pers.vic.upload.login.model.User;
import pers.vic.upload.socket.service.UploadHandlerService;
import pers.vic.upload.utils.SessionHelper;

/**
 * 描述:socket文件断点续传
 * <p>
 *     1. 文件固定大小分片
 *     2. 文件按照分片顺序上传
 *     3. 同一时间只上传一个分片
 * </p>
 * @author Vic.xu
 * @date 2022-06-07 9:51
 */
public class SocketUploadHandler extends AbstractWebSocketHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(SocketUploadHandler.class);

    private final UploadHandlerService uploadHandlerService;


    public SocketUploadHandler(UploadHandlerService uploadHandlerService) {
        this.uploadHandlerService = uploadHandlerService;
    }


    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        Object sessionId = session.getAttributes().get(HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME);
        LOGGER.info("WebSocket连接成功：session={}, HttpSessionHandshakeInterceptor.HTTP_SESSION_ID_ATTR_NAME={}, user={}",
                session.getId(), sessionId, session.getAttributes().get(SessionAttr.USER_NAME));
        LOGGER.info("session attributes: {}", session.getAttributes());
    }


    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        try {
            LOGGER.debug("handleTextMessage , current session id is {}", session.getId());
            SessionHelper.setSocketThreadUser(session);
            String payload = message.getPayload();
            LOGGER.info("handleTextMessage  payload =  {}", payload);
            uploadHandlerService.handleTextMessage(session, message);
        } finally {
            SessionHelper.removeSocketThreadUser();
        }


    }

    @Override
    protected void handleBinaryMessage(WebSocketSession session, BinaryMessage message) throws Exception {
        try {
            LOGGER.debug("handleBinaryMessage , current session id is {}", session.getId());
            SessionHelper.setSocketThreadUser(session);
            int payloadLength = message.getPayloadLength();
            LOGGER.info("handleBinaryMessage  payloadLength =  {}", payloadLength);
            uploadHandlerService.handleBinaryMessage(session, message);
        } finally {
            SessionHelper.removeSocketThreadUser();
        }
    }

    @Override
    public void afterConnectionClosed(WebSocketSession session, CloseStatus status) throws Exception {
        LOGGER.info("WebSocket连接关闭：session={}, status={}, user={}", session, status, status.toString(), session.getAttributes().get(SessionAttr.USER_NAME));
    }
}
